<template>
  <div class="bottom-menu">
    <check-button class="select-all" :isChecked="isSelectAll" @checkBtnClick="checkBtnClick"></check-button>
    <span>全选</span>
    <span class="total-price">合计:{{ totalPrice }}</span>
    <span class="buy-product" @click="settleAccounts">去结算:{{ checkLength }}</span>
  </div>
</template>

<script>
import CheckButton from "components/content/checkButton/CheckButton";
import { mapGetters } from 'vuex'
export default {
  name: "CartBottomBar",
  components: {
    CheckButton
  },
  data() {
    return {};
  },
  methods: {
    checkBtnClick() {
      // let isSelectAll = this.cartList.some(item => !item.checked)
      let isSelectAll = this.isSelectAll
      this.cartList.forEach(item => {
        isSelectAll ? item.checked = false : item.checked = true
      })
    },
    settleAccounts() {
      if (this.checkLength == 0) {
        this.$toast.show('请选择商品')
      }
    }
  },
  computed: {
    ...mapGetters(['cartList']),

    totalPrice() {
      let checked = this.cartList.filter((item)=> {
        return item.checked
      })
      let totalPrices = checked.reduce((total, item)=> {
        return total + item.price * item.count
      }, 0)
      return '¥' + totalPrices.toFixed(2) 
    },

    checkLength() {
      let checked = this.cartList.filter((item)=> {
        return item.checked
      })
      let totalLength = checked.reduce((total, item)=> {
        return total + item.count
      }, 0)
      return totalLength
    },

    isSelectAll() {
      if (this.cartList.length === 0)  return false
      return this.cartList.every(item => item.checked)
    }
  }
};
</script>

<style scoped>
  .bottom-menu {
    width: 100%;
    height: 44px;
    background-color: #eee;
    position: fixed;
    bottom: 49px;
    left: 0;
    box-shadow: 0 -2px 3px rgba(0, 0, 0, .2);
    font-size: 14px;
    color: #888;
    line-height: 44px;
    padding-left: 35px;
    box-sizing: border-box;
  }

  .bottom-menu .select-all {
    position: absolute;
    line-height: 0;
    left: 12px;
    top: 13px;
  }

  .bottom-menu .total-price {
    margin-left: 15px;
    font-size: 16px;
    color: #666;
  }

  .bottom-menu .buy-product {
    background-color: orangered;
    color: #fff;
    width: 100px;
    height: 44px;
    text-align: center;
    line-height: 44px;
    float: right;
  } 
</style>